<?php
Yii::import('lib.widgets.CInputWidgetAdv');
/**
 * xheditor 富文本编辑器
 * 官方网站：{@link http://xheditor.com/}
 * 配置参考：{@link http://www.cnblogs.com/michaelShao/archive/2012/03/01/2375622.html}
 * <pre>
 * <?php $this->widget('lib.widgets.xheditor.CXhEditor', array(
 *  // CActiveRecord 实例
 *  'model' => $model,
 *  // 实例的属性
 *  'attribute' => 'table_name',
 * ))?>
 * </pre>
 * @author yangdongqi <yangdongqi@hayzone.com>
 * @package lib.widgets
 */
class CXhEditor extends CInputWidgetAdv {
	
	/**
	 * 默认的xheditor js配置
	 * <br>如果设置为false或null，该参数不生效
	 * @var mixed
	 */
	public $defaultOptions = array(
		'tools' => 'simple'
	);
	
	/**
	 * xheditor的js配置
	 * @var array
	 */
	public $options = array();
	
	/**
	 * @see CWidget::run()
	 * @return void
	 */
	public function run() {

		$cs = Yii::app()->clientScript;
		$baseUrl = Yii::app()->assetManager->publish(
			dirname(__FILE__) . '/assets/'
		);
		
		$cs->registerScriptFile($baseUrl.'/xheditor-1.1.12-zh-cn.min.js');
		
		list($model, $attribute, $htmlOptions) = $this->resolveArgs();
		CHtml::resolveNameID($model, $attribute, $htmlOptions);
		$id = $htmlOptions['id'];
		
		echo CHtml::activeTextArea($model, $attribute, $htmlOptions);
		
		$options = is_array($this->defaultOptions) 
			? array_merge($this->defaultOptions, $this->options)
			: $this->options;
		$jsOptions = CJSON::encode($options);
		
		$script = "
			jQuery('#{$id}').xheditor({$jsOptions});
		";
		$cs->registerScript('xheditor_'.$id, $script, CClientScript::POS_END);
	}
	
}

?>